home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2005 June (DVD) / DPPRO0605DVD.iso / Install / program files / Borland / BDS / 3.0 / Demos / Delphi.Net / CLR / ProcessViewer / ProcessCheckerSettings.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2004-10-22  |  4.0 KB  |  132 lines

  1. unit ProcessCheckerSettings;
  2. {*******************************************************************************
  3.   ProcessChecker Demo
  4.   Written by David Clegg, davidclegg@optusnet.com.au.
  5.  
  6.   This unit contains the ProcessCheckerSettings class, which is used to store
  7.   the settings for the application.
  8. *******************************************************************************}
  9.  
  10. interface
  11.  
  12. uses
  13.   ProcessClasses, System.Runtime.Serialization;
  14.  
  15. type
  16.     /// <summary>
  17.     /// Class to persist the settings for ProcessChecker. It is marked with the
  18.     /// [Serializable] attribute so we can serialize it to disk. All other
  19.     /// classes exposed as properties of this class must also be marked as
  20.     /// [Serializable], or have their field declarations marked as
  21.     /// [NonSerialized].
  22.     /// </summary>
  23.   [Serializable]
  24.   TProcessCheckerSettings = class
  25.   strict private
  26.     FWatchedProcesses: TProcesses;
  27.     FCheckFrequency: integer;
  28.     FRestartOneProcess: boolean;
  29.     FEnabled: boolean;
  30.         [NonSerialized]
  31.     FSettingsLoaded: EventHandler;
  32.         [NonSerialized]
  33.     FSettingsSaved: EventHandler;
  34.     procedure InitDefaults;
  35.   public
  36.     property WatchedProcesses: TProcesses read FWatchedProcesses;
  37.     property CheckFrequency: integer read FCheckFrequency write FCheckFrequency;
  38.     property RestartOneProcess: boolean read FRestartOneProcess write FRestartOneProcess;
  39.     property SettingsLoaded: EventHandler add FSettingsLoaded remove FSettingsLoaded;
  40.     property SettingsSaved: EventHandler add FSettingsSaved remove FSettingsSaved;
  41.     property Enabled: boolean read FEnabled write FEnabled;
  42.     class function Load: TProcessCheckerSettings;
  43.     procedure Save;
  44.     constructor Create;
  45.   end;
  46.  
  47. implementation
  48.  
  49. uses
  50.   System.Runtime.Serialization.Formatters.Binary, System.Windows.Forms,
  51.   System.IO;
  52.  
  53. const
  54.   CHECK_FREQUENCY = 5;
  55.   RESTART_ONE_PROCESS = True;
  56.   SETTINGS_EXTENSION = 'dat';
  57.  
  58. constructor TProcessCheckerSettings.Create;
  59. begin
  60.   inherited Create;
  61.   FWatchedProcesses := TProcesses.Create;
  62.   InitDefaults;
  63. end;
  64.  
  65. /// <summary>
  66. /// Serialize a ProcessCheckerSettings instance from disk.
  67. /// </summary>
  68. class function TProcessCheckerSettings.Load: TProcessCheckerSettings;
  69. var
  70.   lSettings: TProcessCheckerSettings;
  71.   lFileName: string;
  72.   lFileStream: FileStream;
  73.   lFormatter: BinaryFormatter;
  74. begin
  75.   //The ProcessCheckerSettings will be deserialized from .\ProcessChecker.dat
  76.   lFileName := Path.ChangeExtension(Application.ExecutablePath, SETTINGS_EXTENSION);
  77.   if (System.IO.File.Exists(lFileName)) then
  78.   begin
  79.     lFileStream := FileStream.Create(lFileName, FileMode.Open);
  80.     try
  81.       lFormatter := BinaryFormatter.Create;
  82.       lSettings := TProcessCheckerSettings(lFormatter.Deserialize(lFileStream));
  83.     finally
  84.       lFileStream.Close;
  85.     end;
  86.   end
  87.   else
  88.     lSettings := TProcessCheckerSettings.Create;
  89.  
  90.  
  91.   //Fire the event handler(s) if any delegates have been assigned.
  92.   if Assigned(lSettings.FSettingsLoaded) then
  93.     lSettings.FSettingsLoaded(lSettings, nil);
  94.  
  95.   Result := lSettings;
  96. end;
  97.  
  98. /// <summary>
  99. /// Serialize the ProcessCheckerSettings instance to disk.
  100. /// </summary>
  101. procedure TProcessCheckerSettings.Save;
  102. var
  103.   lFileName: string;
  104.   lFileStream: FileStream;
  105.   lFormatter: BinaryFormatter;
  106. begin
  107.   //The instance will be serialized to .\ProcessChecker.dat
  108.   lFileName := Path.ChangeExtension(Application.ExecutablePath, SETTINGS_EXTENSION);
  109.   lFileStream := FileStream.Create(lFileName, FileMode.Create);
  110.   try
  111.     //Create the FileStream to stream out the .dat file, and
  112.     //serialize using the BinaryFormatter class.
  113.     lFormatter := BinaryFormatter.Create;
  114.     lFormatter.Serialize(lFileStream, Self);
  115.   finally
  116.     lFileStream.Close();
  117.   end;
  118.   //Fire the event handler(s) if any delegates have been assigned.
  119.   if Assigned(FSettingsSaved) then
  120.     FSettingsSaved(Self, nil);
  121. end;
  122.  
  123. procedure TProcessCheckerSettings.InitDefaults;
  124. begin
  125.   FCheckFrequency := CHECK_FREQUENCY;
  126.   FRestartOneProcess := RESTART_ONE_PROCESS;
  127. end;
  128.  
  129. end.
  130.  
  131.  
  132.